package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleSetingService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roleSeting")
public class RoleSetingController {
    @Reference
    private RoleSetingService roleSetingService;
    //获取当前登录（认证）用户的用户名
//添加前进行数据回显
    @RequestMapping("/findAll")
    public Result findAll(){
        try {

            Map all = roleSetingService.findAll();
            return new Result(true,MessageConstant.NULL,all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissions, Integer[] menus){

        try {
            roleSetingService.add(role,permissions,menus);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){

        return roleSetingService.findPage(pageBean);

    }

    @RequestMapping("/editecho")
    public Result editecho(int id){

        try {
            Map role= roleSetingService.editecho(id);
            return new Result(true,MessageConstant.NULL,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissions, Integer[] menus){

        try {
             roleSetingService.edit(role,permissions,menus);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            roleSetingService.delete(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }

    }
}
